import json, csv


# 读取CSV
def readCsv(file):
    rows = []
    with open(file, 'r') as f:
        lst_csv = csv.reader(f)
        for r in lst_csv:
            rows.append(r)
        f.close()
    
    return rows
# 写入CSV
def saveCsv():
    headers = ['class','name','sex','height','year']

    rows = [
            {'class':1,'name':'xiaoming','sex':'male','height':168,'year':23},
            {'class':1,'name':'xiaohong','sex':'female','height':162,'year':22},
            {'class':2,'name':'xiaozhang','sex':'female','height':163,'year':21},
            {'class':2,'name':'xiaoli','sex':'male','height':158,'year':21},
        ]
    with open('playerData.csv','w',newline='')as f:
        f_csv = csv.DictWriter(f,headers)
        f_csv.writeheader()
        f_csv.writerows(rows)
# 读取成dirc
def readJson(file):
    with open(file, 'r') as load_f:
        data=json.load(load_f)
        load_f.close()
        #print("[INFO]: 已读入数据")
    return data
# 保存为json
def saveJson(file,data):
    with open(file, 'w') as load_f:
        jsonStr = json.dumps(data,sort_keys=True, indent=4)
        #print(jsonStr)
        load_f.write(jsonStr)
        load_f.close()
        #print("[INFO]: 已保存数据")
        return True


def loadData(file):
    gameData = readJson(file)
    path = gameData['dataPath']
    ships = readCsv(path + gameData['shipData'])
    player = readJson(path + gameData['playerData'])
    data = {'gameData':gameData, 'ships':ships[1:],'player':player}
    return data

def saveData(file, gamedata, playerdata):
    path = gamedata['dataPath']

    playerFile = gamedata['playerData']
    saveJson(path+ playerFile, playerdata)
    print("玩家数据已保存.")

    print("保存完毕,bye.")
    return True



    # 飞船裸属性     名称n 类型t 能量输出e 速度v 货舱容量cap 外部槽位o 中间槽位m 核心槽位c 护盾s 护甲a 结构h 回盾r
    # 飞船激活属性   以上 + 武器(攻击力, 命中率) + 盾甲(减伤率 回盾率) + 核心(能量输出 速度 )
    # 人物加成      同上
